//
//  HNSH_TextMessageCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/18.
//

#import "HNSH_TextMessageCell.h"

@interface HNSH_TextMessageCell ()
/** */
@property (nonatomic, strong)UILabel * levelLabel;
@end

@implementation HNSH_TextMessageCell

#pragma mark - Cycle Methods
+ (CGSize)sizeForMessageModel:(RCMessageModel *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight{
    CGSize size = [super sizeForMessageModel:model withCollectionViewWidth:collectionViewWidth referenceExtraHeight:extraHeight];
    CGFloat h = (model.messageDirection == MessageDirection_SEND || model.conversationType == ConversationType_PRIVATE) ? 0 : 22;
    return CGSizeMake(collectionViewWidth, size.height + h);
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addAllViews];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    if (self.model.messageDirection == MessageDirection_SEND) {
        self.messageContentView.frame = CGRectMake(self.messageContentView.origin.x, 0, self.messageContentView.width, self.messageContentView.height);
        self.nicknameLabel.hidden =  self.levelLabel.hidden = YES;
        self.bubbleBackgroundView.image = [UIImage imageWithColor:[UIColor colorWithHexString:@"#F9DFDC"]];
    } else {
        if (self.model.conversationType == ConversationType_GROUP) {
            self.messageContentView.frame = CGRectMake(self.messageContentView.origin.x, self.nicknameLabel.height+4, self.messageContentView.width, self.messageContentView.height);
            self.nicknameLabel.hidden = self.levelLabel.hidden = NO;
            [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.nicknameLabel.mas_right).offset(6);
                make.centerY.equalTo(self.nicknameLabel.mas_centerY);
                make.width.mas_equalTo(26);
                make.height.mas_equalTo(17);
            }];
        } else {
            self.messageContentView.frame = CGRectMake(self.messageContentView.origin.x, 0, self.messageContentView.width, self.messageContentView.height);
            self.nicknameLabel.hidden =  self.levelLabel.hidden = YES;
        }
        
    }
    
    RCUserInfo * userInfo = [[RCIM sharedRCIM] getUserInfoCache:self.model.senderUserId];
    if (userInfo) {
        self.nicknameLabel.frame = CGRectMake(self.nicknameLabel.origin.x, self.nicknameLabel.origin.y, [userInfo.name widthForFont:self.nicknameLabel.font], self.nicknameLabel.height);
    } else {
        self.nicknameLabel.frame = CGRectMake(self.nicknameLabel.origin.x, self.nicknameLabel.origin.y, [self.model.userInfo.name widthForFont:self.nicknameLabel.font], self.nicknameLabel.height);
    }
}

#pragma mark - Myself Methods
- (void)addAllViews{
    [self.baseContentView addSubview:self.levelLabel];
    self.nicknameLabel.font = [UIFont systemFontOfSize:12];
}

- (void)setModel:(RCMessageModel *)model{
    [super setModel:model];
    
    //
    RCUserInfo * userInfo = [[RCIM sharedRCIM] getUserInfoCache:model.senderUserId];
    if (userInfo) {
        NSDictionary * dic = [userInfo.extra jsonValueDecoded];
        self.levelLabel.text = [NSString stringWithFormat:@"V%ld",[dic[@"level"] integerValue]];
        self.nicknameLabel.frame = CGRectMake(self.nicknameLabel.origin.x, self.nicknameLabel.origin.y, [userInfo.name widthForFont:self.nicknameLabel.font], self.nicknameLabel.height);
    } else {
        NSDictionary * dic = [model.userInfo.extra jsonValueDecoded];
        self.levelLabel.text = [NSString stringWithFormat:@"V%ld",[dic[@"level"] integerValue]];
        self.nicknameLabel.frame = CGRectMake(self.nicknameLabel.origin.x, self.nicknameLabel.origin.y, [model.userInfo.name widthForFont:self.nicknameLabel.font], self.nicknameLabel.height);
    }
}

#pragma mark - Getter Methods
- (UILabel *)levelLabel{
    if (!_levelLabel) {
        _levelLabel = [UILabel new];
        _levelLabel.font = [UIFont systemFontOfSize:12];
        _levelLabel.textColor = [UIColor appWhiteLabelColor];
        _levelLabel.textAlignment = NSTextAlignmentCenter;
        _levelLabel.backgroundColor = [UIColor colorWithHexString:@"#FB6F1C"];
        _levelLabel.layer.cornerRadius = 4.0f;
        _levelLabel.layer.masksToBounds = YES;
    }
    return _levelLabel;
}

@end
